$(function () {
  var app = {
    openId: GetRequest().openId,
    hostSever: getServerHost(),
    cashPrize: '/api/longtaitou/cashPrize',
    checkCashed: '/api/longtaitou/checkCashed',
    prizeList: '/api/longtaitou/prizeList',
    getUserCount: '/api/longtaitou/getUserCount',
    prized: false, //领取过奖没rp
    totalCount: 0,
    myGragen: -1,
    prizeMap: '',
    init: function () {
      //获取奖品列表
      this.getPrizeList();
      this.checkHavePrized();
      //注册关闭按钮事件
      closeBar('.close-btn', '.setAddr');
      closeBar('.close-btn', '.setPhone');
      closeBar('.closeHave', '.haveprize');
    },

    //点击领奖
    initExchange: function (event) {
      // $('.en').off('click');

      var event = this;
      if (app.prized) {
        autoshowBar('.haveprize');
        // app.getMyPrize();
        return;
      }
      console.log(Number($(event).attr('data-key')));

      if (1 == Number($(event).attr('data-key'))) {
        // $('.setAddr').fadeIn();
        autoshowBar('.setAddr');
      } else if (2 == Number($(event).attr('data-key'))) {
        autoshowBar('.setPhone');
      } else if (3 == Number($(event).attr('data-key'))) {
        $('.en').on('click', app.initExchange);
        location.href = "http://m.xiji.com/wap/newuserrecommend.html"
      }
      $('#sendPhone').on('click', sendMsg)
      $('#sendAddr').on('click', sendMsg)

      function sendMsg() {
        var data = {
          address: "",
          name: "",
          type: "",
          mobile: "",
          openId: app.openId
        }
        if (1 == Number($(event).attr('data-key'))) {
          if (!$('#mUser').val()) {
            alert('请输入姓名');
            return;
          }
          if (!checkMobile($('#aPhone').val())) {
            alert('请输入11位手机号');
            return;
          }
          if (!$('#mAddr').val()) {
            alert('请输入收获地址');
            return;
          }
          data = {
            address: $('#mAddr').val(),
            name: $('#mUser').val(),
            type: 1,
            mobile: $('#aPhone').val(),
            openId: app.openId
          }
        } else if (2 == Number($(event).attr('data-key'))) {
          if (!checkMobile($('#mPhone').val())) {
            alert('请输入11位手机号');
            return;
          }
          data = {
            address: "",
            name: "",
            type: 2,
            mobile: $('#aPhone').val(),
            openId: app.openId
          }
        }

        $.ajax({
          type: 'post',
          url: app.hostSever + app.cashPrize,
          data: JSON.stringify(data),
          contentType: "application/json;charset=UTF-8",
          beforesend: function () {
            $('#sendPhone').off('click');
          },
          complete: function () {
            $('.loading').hide();
            $('#sendPhone').on('click', sendMsg)

          },
          success: function (rp) {
            if (999 == rp.code) {
              alert(rp.msg);
              autocloseBar('.setPhone');
              location.href = self.location;
            } else if (202 == rp.code) {
              $('.haveprize').show();
            } else if (200 == rp.code) {
              $('.en').off('click');
              $('.en').addClass('unen').removeClass('en');
              alert('领取成功');
              //注册关闭按钮事件
              autocloseBar('.setAddr')
              autocloseBar('.setPhone')
              // app.getMyPrize();
            }
            console.log(rp);
          }
        })
      }
    },
    noExchange: function () {
      var now = new Date();
      console.log(now.getDate());
      if (18 == now.getDate()) {
        autoshowBar('.noprize2');
        closeBar('.cprizeBtn', '.noprize2');
      } else {
        autoshowBar('.noprize1');
        closeBar('.cprizeBtn', '.noprize1');
      }
    },
    getPrizeList: function () {
      // 获取打卡信息
      $.get(app.hostSever + app.getUserCount, {
        openId: app.openId
      }).done(function (rp) {
        if (200 == rp.code) {
          $('.loading').hide();
          app.myGragen = rp.res.count;
          setPrize();
        }
      }).fail(function (rp) {})

      function setPrize() {
        $.get(app.hostSever + app.prizeList).done(function (rp) {
          if (200 == rp.code) {
            app.prizeMap = rp.res;
            console.log('222')
            console.log(rp.res);
            rp.res.map(function (item, index) {
              console.log(item, index);
              var list = ['一等奖', '二等奖', '三等奖'];
              var rankCode = [200, 140, 60];
              app.totalCount += Number(item.size);
              item.rank = list[index];
              item.enough = rankCode[index];
              console.log(app.myGragen, rankCode[index], item.size);
              if (app.myGragen >= rankCode[index] && Number(item.size)) {
                item.style = 'en';
              } else {
                item.style = 'unen';
              }
            })
            //模板填充
            var data = {
              firstload: rp.res
            };
            var html = template('template', data);
            $('article ul').html(html);
            //添加绑定事件
          }
          //所有奖品都没有了
          if (!app.totalCount) {
            app.noExchange();
          } else {
            $('.unen').on('click', function () {
              // alert('您当前有' + app.myGragen + '个龙头，不足够领取');
            });
          }
          //为还能兑换的兑换奖品
          $('.en').on('click', app.initExchange);
        }).fail(function (rp) {})
      }
    },
    checkHavePrized: function () {
      $.get(app.hostSever + app.checkCashed, {
        openId: app.openId
      }).done(function (rp) {
        app.prized = rp.res;
        if (app.prized) {
          // app.getMyPrize();
          return;
        }
      }).fail(function (rp) {})
    },
  }
  app.init();
});